<?php
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\admin\logic\GoodsLogic;
use app\admin\model\Goods;

class DesignMenu extends Base{

    public function index(){
//        $type = input('param.type') ? input('param.type') : $this->platform[0];
//        $info = Db::name('designer_menu')->where('platform',$type)->find();
        $info = Db::name('designer_menu')->where('id',1)->find();
        if(!$info){
            $insertdata = [];
            $insertdata['menucount'] = 4;
            $insertdata['indexurl'] = '/pages/index/index';
            $insertdata['menudata'] = jsonEncode([
                "color"=>"#BBBBBB",
                "selectedColor"=>"#FD4A46",
                "backgroundColor"=>"#ffffff",
                "borderStyle"=>"black",
                "position"=>"bottom",
                "list"=>[
                    ["text"=>"首页","pagePath"=>"/pages/index/index","iconPath"=>getCurUrl().'/static/img/tabbar/home.png',"selectedIconPath"=>getCurUrl().'/static/img/tabbar/home2.png',"pagePathname"=>"基础功能>首页"
                    ],
                    ["text"=>"分类","pagePath"=>"/pages/shop/classify","iconPath"=>getCurUrl().'/static/img/tabbar/category.png',"selectedIconPath"=>getCurUrl().'/static/img/tabbar/category2.png',"pagePathname"=>"基础功能>分类商品"
                    ],
                    ["text"=>"购物车","pagePath"=>"/pages/shop/cart","iconPath"=>getCurUrl().'/static/img/tabbar/cart.png',"selectedIconPath"=>getCurUrl().'/static/img/tabbar/cart2.png',"pagePathname"=>"基础功能>购物车"
                    ],
                    ["text"=>"我的","pagePath"=>"/pages/my/usercenter","iconPath"=>getCurUrl().'/static/img/tabbar/my.png',"selectedIconPath"=>getCurUrl().'/static/img/tabbar/my2.png',"pagePathname"=>"基础功能>会员中心"
                    ],
                    ["text"=>"导航名称","pagePath"=>"","iconPath"=>getCurUrl().'/static/img/tabbar/category.png',"selectedIconPath"=>getCurUrl().'/static/img/tabbar/category2.png',"pagePathname"=>""
                    ],
                ]
            ]);
            $insertdata['navigationBarBackgroundColor'] = '#333333';
            $insertdata['navigationBarTextStyle'] = 'white';
            $insertdata['platform'] = 'mp';
            Db::name('designer_menu')->insert($insertdata);
        }
       // dump(json_decode($info['menudata'],true));
        $info = Db::name('designer_menu')->where('id',1)->find();
        $menudata = json_decode(htmlspecialchars_decode($info['menudata']),true);
        //dump($menudata);exit;
        View::assign('menudata',$menudata);
        View::assign('info',$info);
        View::assign('type',$info['type']??'mp');
        return View::fetch();
    }
    public function save(){
        $type = input('param.type') ? input('param.type') : $this->platform[0];
        $data = input('post.info/a');
        $menudata = $data['menudata'];
        $menudata['list'] = array_slice($menudata['list'],0,$data['menucount']);
        $data['menudata'] = htmlspecialchars_decode(json_encode($menudata,JSON_UNESCAPED_UNICODE));
        $data['updatetime'] = time();


            Db::name('designer_menu')->where('id',1)->update($data);

        return json(['status'=>1,'msg'=>'保存成功','url'=>(string)url('index').'/type/'.$type]);
    }
}